﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EntityComponentSystem.Framework
{
    public class IntPool
    {
        private int nextID = 0;
        private Stack<int> free;

        public IntPool()
            : this(4)
        {

        }

        public IntPool(int size)
        {
            nextID = size;
            free = new Stack<int>(size);

            for (int i = size - 1; i >= 0; i--)
                free.Push(i);
        }

        public void Free(int index)
        {
            free.Push(index);
        }

        public int Get()
        {
            if (free.Count == 0)
                return nextID++;

            return free.Pop();
        }
    }
}
